// 引入模块 koa
const Koa = require('koa')

// 创建web服务对象
const app = new Koa()
// let user = null
let user = { username: "张三" }

// 中间件判断登录与否
app.use(async (ctx, next) => {
  if (!user) {
    ctx.body = "权限不足"
    return
  }
  await next()
  ctx.body = `${user.username}${ctx.body}`
})

// 登录成功可以继续下一步
app.use((ctx, next) => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      ctx.body = "有权限访问"
      resolve()
    })
  })
})

// 监听 
app.listen(8080, () => {
  console.log("run on port 8080")
})
